<?php

namespace App\Http\Controllers\Goods;

use App\Http\Controllers\Controller;
use App\Http\Controllers\login\BaseController;
use App\Models\Goods\Category;
use App\Models\Goods\SpecValue;
use App\Models\Goods\Type;
use App\Server\Goods;
use Illuminate\Http\Request;

class GoodsFrameworkController extends BaseController
{
    public function goodsAllAdd(){
        return view('goods.index');
    }
    /**
     * @uses ly
     * POST
     * http://www.test.com/goodsAllAdd
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function goodsAllAdds(Request $request){
        try {
            $data=$request->except(['_token','file']);
            $value_ids=array_keys($data);
            $value_ids_str=implode('_',$value_ids);
            $value_ids=array_unique(explode('_',$value_ids_str));
            $valueInfos=SpecValue::whereIn('id',$value_ids)->select();
            if (count($valueInfos)!=count($value_ids)){
                return response()->json(['code'=>2001,'msg'=>'规格数据错误','data'=>[$value_ids]]);
            }
            Goods::create($data);
            return response()->json(['code'=>200,'msg'=>'添加成功']);
        } catch (\Exception $exception){
            return response()->json(['code'=>2001,'msg'=>'添加失败']);
        }
    }


    /**
     * 多条件搜索
     * @param \Illuminate\Support\Facades\Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function goodsPage(\Illuminate\Support\Facades\Request $request){
        $where=[];
        $goods_name=$request->input('goods_name');
        if (!empty($goods_name)){
            $where[]=['goods_name','like',"%$goods_name%"];
        }
        $cate_id=$request->input('cate_id');
        if (!(empty($cate_id))){
            $where[]=['cate_id','like',"%$cate_id%"];
        }
        $goods=\App\Models\Goods\Goods::where($where)->pagiante(5);
        return response()->json(['code'=>200,'msg'=>'添加成功','data'=>[$goods]]);
    }

    /**
     * 商品分类一级
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     */
    public function goodsOneCate(){
        $one=Category::all();
        $oneLevel=$this->sonLevel($one);//调用递归展示商品一级分类
//        dd($oneLevel);
        return view('goods.index',compact('oneLevel'));
    }

    /**
     * 商品分类二级
     * @param Request $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     */
    public function goodsTwoCate(Request $request){
        $id=$request->all('id');
        $two=Category::where('pid',$id)->first();dd($two);
        return view('goods.index',compact('two'));
    }

    /**
     * 商品三级
     */
    public function goodsThreeCate(){
        dd(123123);
    }
}
